package com.have.blog.admin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.have.blog.admin.domain.dto.BlogDto;
import com.have.blog.admin.domain.dto.SubjectItemDto;
import com.have.blog.admin.service.AdminBlogService;
import com.have.blog.admin.service.SubjectItemService;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.BlogSubjectItemMapper;
import com.have.blog.model.models.BlogSubjectItem;
import com.have.blog.model.models.BlogSubjectItemExample;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("all")
public class SubjectItemServiceImpl implements SubjectItemService {
    @Autowired
    private BlogSubjectItemMapper subjectItemMapper;
    @Autowired
    private AdminBlogService blogService;

    @Override
    public CommonPage<SubjectItemDto> getPageList(SubjectItemDto subjectItemDto) {
        Page page = PageHelper.startPage(subjectItemDto.getPageNum(), subjectItemDto.getPageSize());

        BlogSubjectItemExample subjectItemExample = new BlogSubjectItemExample();
        BlogSubjectItemExample.Criteria criteria = subjectItemExample.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (subjectItemDto.getBlogSubjectUid() != null) {
            criteria.andBlogSubjectUidEqualTo(subjectItemDto.getBlogSubjectUid());
        }
        subjectItemExample.setOrderByClause("sort desc");
        List<BlogSubjectItem> blogSubjectItems = subjectItemMapper.selectByExample(subjectItemExample);
        List<SubjectItemDto> subjectItemDtos = MyBeanUtils.copyListProperties(blogSubjectItems, SubjectItemDto::new);
        CommonPage<SubjectItemDto> subjectItemDtoCommonPage = CommonPage.restPage(subjectItemDtos);
        subjectItemDtoCommonPage.setTotal(page.getTotal());
        List<Integer> blogUids = new ArrayList<>();
        blogSubjectItems.forEach(item -> {
            blogUids.add(item.getBlogUid());
        });
        List<BlogDto> blogByUids = blogService.getBlogByUids(blogUids);
        if (!CollectionUtils.isEmpty(blogByUids)) {
            Map<Integer, BlogDto> blogMap = new HashMap<>();
            for (BlogDto item : blogByUids) {
                blogMap.put(item.getUid(), item);
            }
            for (SubjectItemDto item : subjectItemDtos) {
                item.setBlog(blogMap.get(item.getBlogUid()));
            }
        }

        return subjectItemDtoCommonPage;
    }

    @Transactional
    @Override
    public Wrapper addSubjectItemList(List<SubjectItemDto> list) {
        List<BlogSubjectItem> blogSubjectItems = MyBeanUtils.copyListProperties(list, BlogSubjectItem::new);
        for (BlogSubjectItem blogSubjectItem : blogSubjectItems) {
            blogSubjectItem.setStatus(BlogStatus.ENABLE.getStatus());
            blogSubjectItem.setSort(0);
            subjectItemMapper.insert(blogSubjectItem);
        }
        return Wrapper.success();
    }

    @Override
    public Wrapper editSubjectItemList(List<SubjectItemDto> list) {
        return null;
    }

    @Override
    public Wrapper deleteBatchSubjectItem(List<Integer> uids) {
        return null;
    }

    @Override
    public Wrapper sortByCreateTime(String subjectUid, Boolean isDesc) {
        return null;
    }
}
